package com.wn.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class CORSFilter implements Filter {


    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //response里添加处理允许跨域的响应头信息
        HttpServletResponse httpServletResponse = (HttpServletResponse)response;
        httpServletResponse.addHeader("Access-Control-Allow-Origin","*");  //允许任意源发起跨域请求

        //允许自定义请求头MyExceptionHandler
        httpServletResponse.addHeader("Access-Control-Allow-Headers","*");

        //允许非get或post的请求方式
        httpServletResponse.addHeader("Access-Control-Allow-Methods", "*");

        //放行
        chain.doFilter(request,response);
    }


}
